import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";

/**
 * 存放格式为：
 {
    theme1: theme1ConfigObj,
    theme2: theme2ConfigObg,
    active: 'theme1'
 }
 * @returns {{readLocalTheme: readLocalTheme, setLocalTheme: setLocalTheme}}
 */
export default function useThemeHelper() {
  function setLocalTheme(themeObj) {
    var _objectSpread2;

    var themeStr = localStorage.getItem("themeConfig");
    var oldConfig = {};

    if (themeStr && themeStr !== "undefined") {
      oldConfig = JSON.parse(themeStr);
    }

    var schema = themeObj.current.schema;

    var result = _objectSpread({}, oldConfig, (_objectSpread2 = {}, _defineProperty(_objectSpread2, schema, themeObj), _defineProperty(_objectSpread2, "active", schema), _objectSpread2));

    localStorage.setItem("themeConfig", JSON.stringify(result));
  }

  function readLocalCurrentTheme(callback) {
    var configStr = localStorage.getItem("themeConfig");

    if (configStr && configStr !== "undefined") {
      // 之所以要判断是不是undefined，是因为往本地存储的时候如果传的值不能被格式化，就会变成undefined字符串
      var configObj = JSON.parse(configStr);

      if (configObj.active && configObj[configObj.active]) {
        callback(configObj[configObj.active]);
      }
    }
  }

  function readOriginLocalTheme() {
    var configStr = localStorage.getItem("themeConfig");

    if (configStr && configStr !== "undefined") {
      // 之所以要判断是不是undefined，是因为往本地存储的时候如果传的值不能被格式化，就会变成undefined字符串
      var configObj = JSON.parse(configStr);
      return configObj || {};
    } else return {};
  }

  return {
    setLocalTheme: setLocalTheme,
    readLocalCurrentTheme: readLocalCurrentTheme,
    readOriginLocalTheme: readOriginLocalTheme
  };
}